package com.ssb.admin.common.utils;

import com.ssb.admin.constant.ResponseStatus;
import io.swagger.annotations.ApiModel;

import java.util.HashMap;
import java.util.Map;

@ApiModel
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 8543544919588146068L;

    public R() {
        put("code", 20000);
        put("message", "success");
    }

    public static R error() {
        return error(50000, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(50000, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("message", msg);
        return r;
    }
    public static R error(ResponseStatus rs){
        R r = new R();
        r.put("code", rs.value());
        r.put("message", rs.getMessage());
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("code", 20000);
        r.put("message", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
    public R data( Object value) {
        super.put("data", value);
        return this;
    }
    public R data(String key, Object value) {
        Map<String, Object> map = new HashMap<>(1);
        map.put(key, value);
        super.put("data", map);
        return this;
    }
    public R list(int total, Object datas){
        Map<String, Object> map = new HashMap<>(2);
        map.put("total", total);
        map.put("datas", datas);
        super.put("data", map);
        return this;
    }
}

